<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="file" @change="onFileChange" />
    <!-- 个人信息 -->
    <van-cell title="头像" center is-link @click="$refs.file.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpdateNum = true"
    />
    <van-cell
      title="性别"
      :value="userGender"
      is-link
      @click="isUpdateGender = true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpdateBirthday = true"
    />

    <!-- 编辑昵称弹出层 -->
    <van-popup v-model="isUpdateNum" position="bottom" style="height:100%">
      <!-- update-name 组件 -->
      <update-name
        v-if="isUpdateNum"
        @close="isUpdateNum = false"
        v-model="user.name"
      />
    </van-popup>
    <!-- 编辑性别弹出层 -->
    <van-popup v-model="isUpdateGender" position="bottom">
      <!-- update-gender 组件 -->
      <update-gender
        v-model="user.gender"
        v-if="isUpdateGender"
        @close="isUpdateGender = false"
      />
    </van-popup>
    <!-- 编辑生日弹出层 -->
    <van-popup v-model="isUpdateBirthday" position="bottom">
      <!-- update-birthday 组件 -->
      <update-birthday
        v-if="isUpdateBirthday"
        v-model="user.birthday"
        @close="isUpdateBirthday = false"
      />
    </van-popup>
    <!-- 编辑头像弹出层 -->
    <van-popup v-model="isUpdatePhoto" position="bottom" style="height:100%">
      <!-- update-photo 组件 -->
      <update-photo
        :img="img"
        v-if="isUpdatePhoto"
        @close="isUpdatePhoto = false"
        @update-photo="user.photo = $event"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'
export default {
  name: 'UserProfile',
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  props: {},
  data() {
    return {
      user: {},
      isUpdateNum: false,
      isUpdateGender: false,
      isUpdateBirthday: false,
      isUpdatePhoto: false,
      img: null
    }
  },
  computed: {
    userGender() {
      return this.user.gender === 1 ? '女' : '男'
    }
  },
  watch: {},
  created() {},
  mounted() {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile()
        this.user = data.data
      } catch (error) {
        this.$toast('获取个人信息失败！')
      }
    },
    onFileChange() {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)
      // 展示预览图片弹出层
      this.isUpdatePhoto = true
      // file-input 如果选择同一个文件 不会触发 change 事件
      // 解决方案 每次使用完 清空 value
      this.$refs.file.value = ''
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
